package org.apache.lucene.analysis.kr.demo;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.kr.KoreanAnalyzer;
import org.apache.lucene.analysis.kr.ma.Dictionary;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;


public class Indexer {
	private static void indexFile(IndexWriter writer, File f) throws IOException {
		if (f.isHidden() || !f.exists() || !f.canRead()) {
			return;
		}
		
		System.out.println("Indexing " + f.getCanonicalPath());
		
		Document doc = new Document();

		// demo 패키지의 FileDocument 클래스를 참고할 것!
		
		// 파일 본문을 추가한다. 토근화 해서 저장하되 본문 내용은 저장하지 않는다.
		Field fieldCon = new Field("contents", new FileReader(f));
		doc.add(fieldCon);
		// 파일 경로를 추가한다. 단, 경로를 토큰화 하지 않는다.
		Field fieldPath = new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.UN_TOKENIZED);
		doc.add(fieldPath);
		writer.addDocument(doc);
	}

	private static void indexDirectory(IndexWriter writer, File dir) throws IOException {
		File[] files = dir.listFiles();
		
		for (int i=0; i<files.length; i++) {
			File f = files[i];
			if (f.isDirectory()) {
				indexDirectory(writer, f); // 재귀 호출
			} else if (f.getName().endsWith(".txt")) {
				indexFile(writer, f);
			}
		}
	}
	
	public static int index(File indexDir, File dataDir) throws IOException {
		if (!dataDir.exists() || !dataDir.isDirectory()) {
			throw new IOException(dataDir + " does not exist or is not a directory");
		}

		// 한글형태소에서 사전을 얻는다.
		Dictionary dic = Dictionary.getInstance();
		
		// 새로운 루씬 색인을 만든다.
		//IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
		IndexWriter writer = new IndexWriter(indexDir, new KoreanAnalyzer(dic), true);
		writer.setUseCompoundFile(false);
		
		indexDirectory(writer, dataDir);
		
		int numIndexed = writer.docCount();
		writer.optimize();
		writer.close(); // 색인을 닫는다.
		return numIndexed;
	}

	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			throw new Exception("Usage: java " + Indexer.class.getName()
					+ " <index dir> <data dir>");
		}
		File indexDir = new File(args[0]);
		File dataDir = new File(args[1]);
		
		long start = new Date().getTime();
		int numIndexed = index(indexDir, dataDir);
		long end = new Date().getTime();
		
		System.out.println("Indexing " + numIndexed + " files took " +
				(end - start) + " milliseconds");
	}

}
